package com.ruoyi.common.utils;

import java.awt.*;
import java.util.Random;

public class ColorUtils {

    /** 获取指定长度的16进制字符串. */
    public static String randomHexStr(int len) {
        try {
            StringBuffer result = new StringBuffer();
            for(int i=0;i<len;i++) {
                //随机生成0-15的数值并转换成16进制
                result.append(Integer.toHexString(new Random().nextInt(16)));
            }
            return result.toString().toUpperCase();
        } catch (Exception e) {
            System.out.println("获取16进制字符串异常，返回默认...");
            return "00CCCC";
        }
    }

    public static Color randomColor() {
        int color = Integer.valueOf(randomHexStr(6), 16);
        return new Color(color);
    }



    public static Color String2Color(String str) {
        int i =   Integer.parseInt(str.substring(1),16);
        return new Color(i);
    }
    public static String Color2String(Color color) {
        String R = Integer.toHexString(color.getRed());
        R = R.length()<2?('0'+R):R;
        String B = Integer.toHexString(color.getBlue());
        B = B.length()<2?('0'+B):B;
        String G = Integer.toHexString(color.getGreen());
        G = G.length()<2?('0'+G):G;
        return '#'+R+B+G;
    }

}
